﻿namespace SearchTxt
{
    class program
    {
        //当前文件数量
        static int sum = 0;
        static void Main(string[] args)
        {
            string C = @"C:\";
            string D = @"D:\";
            Search(C);
            Search(D);
            Console.WriteLine($"电脑中的txt文档有{sum}个");

        }
        static void Search(string path)
        {   
            
            //查找该路径下的txt文件
            string[] file = Directory.GetFiles(path, "*.txt");
            sum += file.Length;
            foreach (string var in file)
                Console.WriteLine(var);
           
            //找出所有子文件夹
            string[] folder = Directory.GetDirectories(path);
            //对子文件夹中的txt文件进行查找
            for (int i = 0; i < folder.Length; i++)
                //避免拒绝访时跳到下一步循环
                try
                {
                    Search(folder[i]);
                }
                catch
                {
                    continue;
                }
            
        }

    }




}



